package com.lanlian.cayman.view;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.utillib.DateUtils;
import com.lanlian.utillib.ToastUtils;

import java.util.Date;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/28 15:57
 *     desc   : 明细筛选框
 *     version: 1.0
 * </pre>
 */
public class DetailsFilterWindow extends PopupWindow implements View.OnClickListener {

    private View mContentView;
    private RadioGroup mRgTime;
    private RadioButton mRbToday;
    private RadioButton mRbYesterday;
    private RadioButton mRbWeek;
    private RadioButton mRbMonth;
    private TextView mTvBeginTime;
    private TextView mTvEndTime;
    private TextView mTvFinish;
    private TextView mTvCancel;
    private View mView;

    private Context mContext;
    private long mBeginDate;
    private long mEndDate;
    private OnFilterListener mOnFilterListener;
    private DateChooseDialog mEndDateDialog;
    private DateChooseDialog mBeginDateDialog;

    public interface OnFilterListener {
        void onFilter(long beginDate, long endDate);
    }

    public DetailsFilterWindow(Context context, OnFilterListener onFilterListener) {
        this.mContext = context;
        this.mOnFilterListener = onFilterListener;
        initView();
        setListener();
        setPopupWindow();
    }

    private void initView() {
        mContentView = LayoutInflater.from(mContext).inflate(R.layout.popup_trade_details_deal_filter, null);
        mRgTime = mContentView.findViewById(R.id.rg_time);
        mTvBeginTime = mContentView.findViewById(R.id.tv_begin_time);
        mTvEndTime = mContentView.findViewById(R.id.tv_end_time);
        mTvFinish = mContentView.findViewById(R.id.tv_finish);
        mTvCancel = mContentView.findViewById(R.id.tv_cancel);
        mRbToday = mContentView.findViewById(R.id.rb_today);
        mRbYesterday = mContentView.findViewById(R.id.rb_yesterday);
        mRbWeek = mContentView.findViewById(R.id.rb_week);
        mRbMonth = mContentView.findViewById(R.id.rb_month);
        mView = mContentView.findViewById(R.id.view);
        mTvBeginTime.setText(DateUtils.getDate(new Date()));
        mTvEndTime.setText(DateUtils.getDate(new Date()));
        mBeginDate = System.currentTimeMillis();
        mEndDate = System.currentTimeMillis();
    }

    private void setPopupWindow() {
        setContentView(mContentView);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new ColorDrawable(0x00000000));
        setAnimationStyle(R.style.animFromTop);
        setOutsideTouchable(false);
        setFocusable(true);
    }

    private void setListener() {
        mTvCancel.setOnClickListener(this);
        mTvFinish.setOnClickListener(this);
        mTvBeginTime.setOnClickListener(this);
        mTvEndTime.setOnClickListener(this);
        mView.setOnClickListener(this);
        mRbMonth.setOnClickListener(this);
        mRbWeek.setOnClickListener(this);
        mRbToday.setOnClickListener(this);
        mRbYesterday.setOnClickListener(this);
        mRgTime.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_today:
                        Date today = new Date();
                        mOnFilterListener.onFilter(today.getTime(), today.getTime());
                        break;
                    case R.id.rb_yesterday:
                        Date yesterday = new Date();
                        yesterday = DateUtils.addDay(yesterday, -1);
                        mOnFilterListener.onFilter(yesterday.getTime(), yesterday.getTime());
                        break;
                    case R.id.rb_week:
                        Date week = new Date();
                        Date week2 = DateUtils.addDay(week, -6);
                        mOnFilterListener.onFilter(week2.getTime(), week.getTime());
                        break;
                    case R.id.rb_month:
                        Date month = new Date();
                        Date month2 = DateUtils.addDay(month, -29);
                        mOnFilterListener.onFilter(month2.getTime(), month.getTime());
                        break;
                    default:
                        break;
                }
                dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.view:
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_finish:
                if (Math.abs(mBeginDate - mEndDate) / 1000 > 30 * 24 * 60 * 60) {
                    ToastUtils.show(mContext, "时间跨度不能超过30天");
                    return;
                }
                if (mBeginDate == 0 || mEndDate == 0) {
                    mOnFilterListener.onFilter(System.currentTimeMillis(), System.currentTimeMillis());
                    dismiss();
                    return;
                }
                if (mBeginDate >= mEndDate) {
                    mOnFilterListener.onFilter(mEndDate, mBeginDate);
                } else {
                    mOnFilterListener.onFilter(mBeginDate, mEndDate);
                }
                dismiss();
                break;
            case R.id.tv_begin_time:
                if (mBeginDateDialog == null) {
                    mBeginDateDialog = new DateChooseDialog(mContext, new DateChooseDialog.OnConfirmListener() {
                        @Override
                        public void onConfirmClick(String timeStr, long time) {
                            mTvBeginTime.setText(timeStr);
                            mBeginDate = time;
                        }
                    });
                }
                mBeginDateDialog.showDialog();
                break;
            case R.id.tv_end_time:
                if (mEndDateDialog == null) {
                    mEndDateDialog = new DateChooseDialog(mContext, new DateChooseDialog.OnConfirmListener() {
                        @Override
                        public void onConfirmClick(String timeStr, long time) {
                            mTvEndTime.setText(timeStr);
                            mEndDate = time;
                        }
                    });
                }
                mEndDateDialog.showDialog();
                break;
            case R.id.rb_today:
                if (mRbToday.isChecked()) {
                    Date today = new Date();
                    mOnFilterListener.onFilter(today.getTime(), today.getTime());
                    dismiss();
                }
                break;
            case R.id.rb_yesterday:
                if (mRbYesterday.isChecked()) {
                    Date yesterday = new Date();
                    yesterday = DateUtils.addDay(yesterday, -1);
                    mOnFilterListener.onFilter(yesterday.getTime(), yesterday.getTime());
                    dismiss();
                }
                break;
            case R.id.rb_week:
                if (mRbWeek.isChecked()) {
                    Date week = new Date();
                    Date week2 = DateUtils.addDay(week, -6);
                    mOnFilterListener.onFilter(week2.getTime(), week.getTime());
                    dismiss();
                }
                break;
            case R.id.rb_month:
                if (mRbMonth.isChecked()) {
                    Date month = new Date();
                    Date month2 = DateUtils.addDay(month, -29);
                    mOnFilterListener.onFilter(month2.getTime(), month.getTime());
                    dismiss();
                }
                break;
            default:
                break;
        }
    }

    public void showPopWindow(View anchor, int xoff, int yoff) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
            showAsDropDown(anchor, xoff, yoff);
        } else {
            showAsDropDown(anchor, xoff, yoff);
        }
    }

}
